﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using DiscoveryLogic.Common.Utilities;

namespace DiscoveryLogic.Analytics.DataObjects
{
    public class Document:EntityBase
    {
        #region props
        private int _DocID;
        /// <summary></summary>
        public int DocID
        {
            get
            {
                return this._DocID;
            }
            set
            {
                this._DocID = value;
            }
        }

        private string _Title;
        /// <summary></summary>
        public string Title
        {
            get
            {
                return this._Title;
            }
            set
            {
                this._Title = value;
            }
        }

        private string _DocContent;
        /// <summary></summary>
        public string DocContent
        {
            get
            {
                return this._DocContent;
            }
            set
            {
                this._DocContent = value;
            }
        }
        #endregion

        #region ctor
        /// <summary>
        /// 
        /// </summary>
        public Document() 
            : base(EntityType.Document, "DocID","Title")
        {
        }
        #endregion

        #region Overrides of EntityBase


        /// <summary>
        /// 
        /// </summary>
        /// <param name="xDoc"></param>
        /// <param name="entityNode"></param>
        public override void Save(ref XmlDocument xDoc, XmlNode entityNode)
        {
            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode, "Score", this.Score.ToString());
            XmlDataUtil.UpdateAttribute(
                ref xDoc, entityNode, "Flag", string.IsNullOrEmpty(this.Flag) ? "" : this.Flag.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode, "DocID",this.DocID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode, "Title", this.Title);
            string encodedContent = Base64Util.EncodeAsBase64(this.DocContent, false);
            XmlDataUtil.AddCdataElement(ref xDoc, entityNode, "DocContent", encodedContent);
        }

        #endregion

        public static Document Read(XmlNode docNode)
        {
            Document doc=new Document();
            doc.Score = double.Parse(XmlDataUtil.GetAttributeValue(docNode, "Score", "0"));
            doc.Flag = XmlDataUtil.GetAttributeValue(docNode, "Flag", "");
            doc.DocID = int.Parse(XmlDataUtil.GetAttributeValue(docNode, "DocID", "0"));
            doc.Title = XmlDataUtil.GetAttributeValue(docNode, "Title", "");
            XmlNode docContentNode = docNode.ChildNodes[0];
            string encodedContent = XmlDataUtil.GetCdataValue(docContentNode, "");
            doc.DocContent = Base64Util.DecodeAsString(encodedContent, false);
            return doc;
        }

        public static List<EntityBase> ReadDocs(XmlDocument xDoc)
        {
            List<EntityBase> docs = new List<EntityBase>();
            XmlNodeList docNodes = xDoc.DocumentElement.SelectNodes("//Doc");
            if (docNodes != null && docNodes.Count > 0)
            {
                foreach (XmlNode docNode in docNodes)
                {
                    Document doc = Document.Read(docNode);
                    docs.Add(doc);
                }
            }
            return docs;
        }
    }
}
